/*
 * AtomicCache.h
 *
 *  Created on: 2011-7-8
 *      Author: gexiao
 */
/*
 * Mutex-free cache for key1+key2=value.
 */
#ifndef ATOMICCACHE_H_
#define ATOMICCACHE_H_
#include "Common.h"
/*
 * One entry in the cache. We store two keys (e.g. the classes that are
 * arguments to "instanceof") and one result (e.g. a boolean value).
 *
 * Must be exactly 16 bytes.
 */
typedef struct AtomicCacheEntry {
	u4 key1;
	u4 key2;
	u4 value;
	volatile u4 version; //version and lock flag
} AtomicCacheEntry;

/*
 *
 */
typedef struct AtomicCache {
	AtomicCacheEntry* entries; //array of entries
	int numEntries; //#of entries, must be power of 2

	void* entryAlloc; //memory allocated for entries

	//cache stats; note we don't guarantee atomic increments for these
	int trivial; //cache access not required
	int fail; //contention failure
	int hits; //found entry in cache
	int misses; //entry was for other keys
	int fills; //entry was empty
} AtomicCache;

/*
 * Allocate a cache.
 */
AtomicCache* dvmAllocAtomicCache(int numEntries);

/*
 * Free a cache.
 */
void dvmFreeAtomicCache(AtomicCache* cache);
#endif /* ATOMICCACHE_H_ */
